package com.paic.mhis.hcpms.assessment.intergration.sao.impl;

import java.math.BigDecimal;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.paic.mhis.hcpms.assessment.common.AssessmentConstants;
import com.paic.mhis.hcpms.assessment.dto.KpiDataDTO;
import com.paic.mhis.hcpms.assessment.intergration.dao.KpiDataDAO;
import com.paic.mhis.hcpms.assessment.intergration.sao.FwaKpiSao;
import com.paic.pafa.app.integration.sa.PafaSAOException;
import com.paic.pafa.appclient.IServiceClient;
import com.paic.pafa.appclient.ServiceParams;
import com.paic.pafa.appclient.ServiceResults;
import com.paic.pafa.appclient.annotation.ActionClient;
import com.paic.pafa.biz.sao.BaseSAO;

/**
 * 综合评价 fwa接口 实现类
 * @author EX-XIEQI001
 *
 */
@Component("fwaKpiSao")
public class FwaKpiSaoImpl extends BaseSAO implements FwaKpiSao {

	// 在不同weblogic下调用
//	@ActionClient(name = "mhis.fwa.medicalOrgInfoESA", pafaAc = "${mhis.fwa.pafaAc}")
	// 在同一个weblogic下调用
	 @ActionClient(name = "mhis.fwa.medicalOrgInfoESA", pafaAc =
	 "${web.defaultPafaAcClient}")
	private IServiceClient serviceClient;

	@Autowired
	private KpiDataDAO kpiDataDAO;

	@Override
	public void invokeMedicalOrgInfoESA(String regionCode,
			String healthcareProviderCode, String kpiTime)
			throws PafaSAOException {
		ServiceParams params = ServiceParams.newInstance();
		params.set("healthcareProviderCode", healthcareProviderCode);
		params.set("kpiTime", kpiTime);
		ServiceResults result = serviceClient.invoke(params);

		Map<String, Object> map = (Map<String, Object>) result.get("map");

		// 违规
		String outOfLineValue = "0.00";
		if (null != map.get("outOfLine")) {
			outOfLineValue = map.get("outOfLine").toString();
		}
		this.saveOrUpdateKpiData(regionCode, AssessmentConstants.IND01010301,
				healthcareProviderCode, kpiTime, new BigDecimal(outOfLineValue));

		// 高度可疑
		String highlySuspiciousValue = "0.00";
		if (null != map.get("highlySuspicious")) {
			highlySuspiciousValue = map.get("highlySuspicious").toString();
		}
		this.saveOrUpdateKpiData(regionCode, AssessmentConstants.IND01010302,
				healthcareProviderCode, kpiTime, new BigDecimal(
						highlySuspiciousValue));

		// 一般可疑
		String suspiciousValue = "0.00";
		if (null != map.get("suspicious")) {
			suspiciousValue = map.get("suspicious").toString();
		}
		this.saveOrUpdateKpiData(regionCode, AssessmentConstants.IND01010303,
				healthcareProviderCode, kpiTime,
				new BigDecimal(suspiciousValue));

	}

	/**
	 *  单条新增或者修改指标数据
	 * @param indicatorCode 指标编码
	 * @param healthcareProviderCode  机构编码
	 * @param kpiTime  时间
	 * @param value   指标值
	 */
	private void saveOrUpdateKpiData(String regionCode, String indicatorCode,
			String healthcareProviderCode, String kpiTime, BigDecimal value) {
		// 赋值
		KpiDataDTO kpiDataDTO = new KpiDataDTO();

		// 统筹区编码
		kpiDataDTO.setRegionCode(regionCode);

		kpiDataDTO.setHealthcareProviderCode(healthcareProviderCode);
		// 统计期间维度(13:月)
		kpiDataDTO.setPeriod(AssessmentConstants.MONTH);
		// 就诊类型 总
		kpiDataDTO.setVisitType(AssessmentConstants.VISIT_ALL);

		// 指标编码
		kpiDataDTO.setIndicatorCode(indicatorCode);

		kpiDataDTO.setKpiTime(kpiTime);
		kpiDataDTO.setIndicatorValue(value);
		kpiDataDTO.setIsValid(AssessmentConstants.IS_VALID_0);

		// 判断数据是否已经存在 如果存在则更新指标值 否则插入数据
		KpiDataDTO newKpiDataDTO = kpiDataDAO.queryKpiData(kpiDataDTO);
		if (null != newKpiDataDTO) {
			newKpiDataDTO.setIndicatorValue(kpiDataDTO.getIndicatorValue());
			kpiDataDAO.updateKpiData(newKpiDataDTO);
		} else {
			kpiDataDAO.insertKpiData(kpiDataDTO);
		}
	}
}
